<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<jsp:include page="../inc.jsp"></jsp:include>
<script type="text/javascript" charset="utf-8">
	var datagrid;
	var auth = "${auths}";
	var buttons = [];
	$(function() {
		if(auth=="all"){
			buttons.push({text : '增加',iconCls : 'icon-add',handler : function() {append();}});
			buttons.push('-');
			buttons.push({text : '删除',iconCls : 'icon-remove',handler : function() {remove();}});
			buttons.push('-');
			buttons.push({text : '取消选中',iconCls : 'icon-undo',handler : function() {datagrid.datagrid('clearSelections');datagrid.datagrid('unselectAll');}});
			buttons.push('-');
		}else if(auth!=""){
			var auths = auth.split(",");
			if(auths.length>0){
				for(var i=0;i<auths.length;i++){
					if(auths[i]=="WDDLS_WDDLS_ADD"){
						buttons.push({text : '增加',iconCls : 'icon-add',handler : function() {append();}});
						buttons.push('-');
					}else if(auths[i]=="WDDLS_WDDLS_DELETE"){
						buttons.push({text : '删除',iconCls : 'icon-remove',handler : function() {remove();}});
						buttons.push('-');
					}
				}
				buttons.push({text : '取消选中',iconCls : 'icon-undo',handler : function() {datagrid.datagrid('clearSelections');datagrid.datagrid('unselectAll');}});
				buttons.push('-');
			}
		}

		datagrid = $('#datagrid').datagrid({
			url : 'agencyAction!datagrid.html',
			title : '我的代理商',
			iconCls : 'icon-save',
			pagination : true,
			pagePosition : 'bottom',
			pageSize : 10,
			pageList : [ 10, 20, 30, 40 ],
			fit : true,
			fitColumns : false,
			nowrap : false,
			border : false,
			idField : 'id',
			sortName : 'id',
			sortOrder : 'desc',
			checkOnSelect : false,
			selectOnCheck : false,
			singleSelect : true,
			columns : [ [
			{
				title : '编号',
				field : 'id',
				width : 150,
				sortable : true,
				checkbox : true
			},{
				title : '关系id',
				field : 'dgrId',
				width : 80,
				hidden : true
			},{
				title : '经销商用户名',
				field : 'username',
				width : 80,
				sortable : true
			},{
				title : '联系人',
				field : 'realName',
				width : 70
			},{
				title : '手机',
				field : 'mobile',
				width : 80
			} ,{
				title : '地址',
				field : 'adress',
				width : 150
			} ,{
				title : '是否建立关联',
				field : 'isGranted',
				width : 150,
				formatter : function(value, row, index) {
					if (row.isGranted) {
						return '关系已建立';
					} else{
						return '点此建立关系：<img onclick="grantConfirm(\''+row.dgrId+'\');" style="cursor:pointer;" src="${pageContext.request.contextPath}/style/images/extjs_icons/tick.png"/>';
					}
				}
			} ,{
				title : '品牌Id',
				field : 'brandids',
				width : 150,
				hidden: true
			},{
				title : '品牌名称',
				field : 'brandname',
				width : 150,
				hidden: true
			} ,{
				title : '关系发起时间',
				field : 'initiationTime',
				width : 150
			} ,{
				title : '关系确认时间',
				field : 'grantTime',
				width : 150
			} ] ],
			toolbar : buttons,
			onRowContextMenu : function(e, rowIndex, rowData) {
				e.preventDefault();
				$(this).datagrid('unselectAll');
				$(this).datagrid('selectRow', rowIndex);
				$('#menu').menu('show', {
					left : e.pageX,
					top : e.pageY
				});
			}
		});
	});
	
	function grantConfirm(dgrId){
		parent.sy.messagerConfirm('请确认', '您要确认当前所选项目？', function(r) {
			if (r) {
				$.ajax({
					url : '${pageContext.request.contextPath}/dls/agencyAction!grantConfirm.html',
					data : {id:dgrId},
					dataType : 'json',
					success : function(d) {
						datagrid.datagrid('load');
						datagrid.datagrid('unselectAll');
						parent.sy.messagerShow({
							title : '提示',
							msg : d.msg
						});
					}
				});
			}
		});
	}
	
	function append() {
		var p = parent.sy.dialog({
			title : '添加代理商',
			href : '${pageContext.request.contextPath}/dls/agencyAction!agencyAdd.html',
			width : 270,
			height : 115,
			buttons : [ {
				text : '添加',
				handler : function() {
					var f = p.find('form');
					f.form('submit', {
						url : '${pageContext.request.contextPath}/dls/agencyAction!add.html',
						success : function(d) {
							var json = $.parseJSON(d);
							if (json.success) {
								datagrid.datagrid('reload');
								p.dialog('close');
							}
							parent.sy.messagerShow({
								msg : json.msg,
								title : '提示'
							});
						}
					});
				}
			} ],
			onLoad : function() {
				var f = p.find('form');
				var aid = f.find('input[name=aid]');
				var aidCombobox = aid.combobox({
					url : '${pageContext.request.contextPath}/qxgl/userAction!noAuth_newAgencyCombobox.html',
					valueField : 'id',
					textField : 'username',
					multiple : false,
					editable : false,
					panelHeight : 'auto',
					onLoadSuccess : function() {
						parent.$.messager.progress('close');
					}
				});
			}
		});
	}
	function remove() {
		var rows = datagrid.datagrid('getChecked');
		var ids = [];
		if (rows.length > 0) {
			parent.sy.messagerConfirm('请确认', '您要删除当前所选项目？', function(r) {
				if (r) {
					for ( var i = 0; i < rows.length; i++) {
						ids.push(rows[i].id);
					}
					$.ajax({
						url : '${pageContext.request.contextPath}/dls/agencyAction!delete.html',
						data : {
							ids : ids.join(',')
						},
						dataType : 'json',
						success : function(d) {
							datagrid.datagrid('load');
							datagrid.datagrid('unselectAll');
							parent.sy.messagerShow({
								title : '提示',
								msg : d.msg
							});
						}
					});
				}
			});
		} else {
			parent.sy.messagerAlert('提示', '请勾选要删除的记录！', 'error');
		}
	}
	function _search() {
		datagrid.datagrid('load', sy.serializeObject($('#searchForm')));
	}
	
	function cleanSearch() {
		datagrid.datagrid('load', {});
		$('#searchForm input').val('');
	}
</script>
</head>
<body class="easyui-layout" data-options="fit:true">
	<div data-options="region:'center',border:false" style="overflow: hidden;">
		<table id="datagrid"></table>
	</div>
	<div id="menu" class="easyui-menu" style="width:120px;display: none;">
		<k:security key="WDDLS_WDDLS_ADD">
			<div onclick="append();" data-options="iconCls:'icon-add'">增加</div>
		</k:security>
		<k:security key="WDDLS_WDDLS_DELETE">
			<div onclick="remove();" data-options="iconCls:'icon-remove'">删除</div>
		</k:security>
	</div>
</body>
</html>